<!--
 * @Descripttion:
 * @version:
 * @Author: 周子尧
 * @Date: 2021-02-07 09:16:07
 * @LastEditors: 周子尧
 * @LastEditTime: 2021-02-07 09:27:51
-->
<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @row-contextmenu="tabRowContextmenu"
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="Title">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="Author" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Pageviews" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.pageviews }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="Status" width="110" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="Display_time" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.display_time }}</span>
        </template>
      </el-table-column>
    </el-table>
    <right-click-menu
      ref="right_click_menu"
      :options="tableMenus"
      @callBack="testCallBack"
    />
  </div>
</template>

<script>
import { getList } from '@/api/table'
import RightClickMenu from '@/components/RightClickMenu'
const tableMenus = [
  {
    value: 'a',
    label: '操作a'
  },
  {
    value: 'b',
    label: '操作b'
  },
  {
    value: 'c',
    label: '操作c',
    children: [
      {
        value: 'c-1',
        label: '操作c-1'
      }
    ]
  }
]
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  components: {
    RightClickMenu
  },
  data() {
    return {
      list: null,
      listLoading: true,
      tableMenus: tableMenus
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        this.list = response.data.items
        this.listLoading = false
      })
    },
    // 表格右击
    tabRowContextmenu(row, column, event) {
      this.$refs.right_click_menu.showMenu(row, event)
    },
    testCallBack(type, data) {
      console.log(`操作方式:${type}`, data)
      this.$message.success(`操作${type}`)
    }
  }
}
</script>
